﻿using System;
using System.Collections.Generic;
using ControleRepresentanteSync.Application;

namespace ControleRepresentanteSync.View
{
    public class ViewModelFactory : IViewModelFactory
    {
        private readonly Dictionary<Type, Func<IViewModel>> m_ViewModels;

        public ViewModelFactory()
        {
            m_ViewModels = new Dictionary<Type, Func<IViewModel>>();
        }

        public void Register<TIViewModel, TViewModel>()
            where TViewModel : TIViewModel
            where TIViewModel : IViewModel
        {
            Register<TIViewModel>(() => (TViewModel)Activator.CreateInstance(typeof (TViewModel)));
        }

        public void Register<TIViewModel>(Func<TIViewModel> func) 
            where TIViewModel : IViewModel
        {
            m_ViewModels.Add(typeof(TIViewModel), () => func());
        }

        public TIViewModel CreateViewModel<TIViewModel>() where TIViewModel : IViewModel
        {
            return (TIViewModel) m_ViewModels[typeof (TIViewModel)]();
        }
    }
}
